package lumis.service.portalmanagement.serviceinstance;

import lumis.doui.table.filter.TableSourceFilter;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.util.ITransaction;
import lumis.util.query.IQueryValue;
import lumis.util.query.QueryValue;

/**
 * {@link TableSourceFilter} extension that returns as {@link IQueryValue value}
 * the service instance identifier of the caller service interface instance.
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class FileManagementPropertiesInterfaceFilter extends TableSourceFilter
{
	private IQueryValue value = null;

	@Override
	public IQueryValue getFilterValue()
	{
		if (value != null)
			return value;
		try
		{
			final String callerServiceInterfaceInstanceId = getSource().getSourceContext().getDouiContext().getRequest()
					.getCallerServiceInterfaceInstanceId();
			ITransaction transaction = getSource().getDouiContext().getTransaction();
			final String serviceInstanceId = ManagerFactory.getServiceInterfaceInstanceManager().get(
					SessionConfig.getCurrentSessionConfig(), callerServiceInterfaceInstanceId, transaction).getServiceInstanceId();
			
			final QueryValue queryValue = new QueryValue(serviceInstanceId);
			value = queryValue;
			setFilterValue(queryValue);
			return queryValue;
		}
		catch (PortalException e)
		{
			throw new RuntimeException(e);
		}
	}
}
